package com.bsg.upm.controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.bsg.upm.annotation.Auth;
import com.bsg.upm.constant.UserRoleConstants;
import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.HostService;

/**
 * 主机管理控制层
 * 
 * @author HCK
 *
 */
@Auth(value = { UserRoleConstants.ADMIN })
@Controller
@RequestMapping("/v1.0/hosts")
public class HostController {

	@Resource
	private HostService hostService;

	/**
	 * 【查询】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public RespJson listHost(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			Map<String, Object> searchParamMap = new HashMap<String, Object>();
			if (paramMap.containsKey("site")) {
				searchParamMap.put("siteId", paramMap.get("site"));
			}
			respJson = hostService.list(searchParamMap);
			if (respJson.getStatus() != HttpStatus.SC_OK) {
				response.setStatus(respJson.getStatus());
			}
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【详情】
	 * 
	 * @param hostId
	 *            主机编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{host}", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getHost(@PathVariable("host") String hostId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = hostService.get(hostId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【注册】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public RespJson registerHost(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = hostService.save(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【编辑】
	 * 
	 * @param hostId
	 *            区域编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{host}", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateHost(@PathVariable("host") String hostId, @RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("hostId", hostId);
			respJson = hostService.update(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
	
	/**
	 * 【编辑】
	 * 
	 * @param hostId
	 *            区域编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{host}/mgm", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateMgmHost(@PathVariable("host") String hostId, @RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("hostId", hostId);
			respJson = hostService.updateMgmHost(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【入库】
	 * 
	 * @param hostId
	 *            主机编码
	 * @return 操作结果
	 */
	@RequestMapping(value = "/{host}/input", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson inputHost(@PathVariable("host") String hostId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = hostService.input(hostId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【出库】
	 * 
	 * @param hostId
	 *            主机编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{host}/output", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson outputHost(@PathVariable("host") String hostId, @RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("id", hostId);
			respJson = hostService.output(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【启用】
	 * 
	 * @param hostId
	 *            主机编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{host}/enable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson enableHost(@PathVariable("host") String hostId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = hostService.enable(hostId, true);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【停用】
	 * 
	 * @param hostId
	 *            主机编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{host}/disable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson disableHost(@PathVariable("host") String hostId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = hostService.enable(hostId, false);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【注销】
	 * 
	 * @param hostId
	 *            主机编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{host}", method = RequestMethod.DELETE)
	@ResponseBody
	public RespJson deRegisterHost(@PathVariable("host") String hostId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = hostService.remove(hostId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 批量注册
	 * 
	 * @param siteId
	 *            站点编码
	 * @param file
	 *            上传文件
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping("/upload")
	@ResponseBody
	public RespJson upload(@RequestParam(value = "site", required = true) String siteId,
			@RequestParam("file") MultipartFile file, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = hostService.upload(siteId, file);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 主机下载
	 * 
	 * @param siteId
	 *            站点编码
	 * @param request
	 *            HttpServletRequest对象
	 * @param response
	 *            HttpServletResponse对象
	 */
	@RequestMapping("/download")
	public void download(@RequestParam(value = "site", required = true) String siteId, HttpServletRequest request,
			HttpServletResponse response) {
		OutputStream os = null;
		Workbook wb = null;
		try {
			String filePath = request.getSession().getServletContext().getRealPath("WEB-INF/file/") + File.separator
					+ "host_download_template.xls";
			String fileName = java.net.URLEncoder.encode("主机信息", "UTF-8");
			response.setContentType("application/vnd.ms-excel");
			response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
			os = response.getOutputStream();
			wb = hostService.download(filePath, siteId);
			wb.write(os);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.flush();
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (wb != null) {
				try {
					wb.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
